package com.prothink.threadproject.controller;

import cloud.tianai.captcha.application.ImageCaptchaApplication;
import cloud.tianai.captcha.application.vo.CaptchaResponse;
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.common.response.ApiResponse;
import cloud.tianai.captcha.validator.common.model.dto.MatchParam;
import com.prothink.threadproject.entity.User;
import com.prothink.threadproject.service.UserService;
import com.prothink.threadproject.vo.ChaptchaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ImageCaptchaApplication application;

    // 创建用户
    @PostMapping("/create")
    public int createUser(@RequestBody User user) {
        return userService.createUser(user);
    }

    // 删除用户
    @DeleteMapping("/delete/{id}")
    public int deleteUser(@PathVariable Long id) {
        return userService.deleteUserById(id);
    }

    /**
     * 生成验证码
     */
    @PostMapping("/validChaptcha")
    public ApiResponse validChaptcha(@RequestBody ChaptchaVo account){
        ApiResponse<?> valid = application.matching(account.getId(), new MatchParam(account.getData()));
        return valid;
    }
    @PostMapping("/v2/chaptcha")
    public CaptchaResponse generatorChaptcha(){
        // 1.生成滑块验证码(该数据返回给前端用于展示验证码数据)
        CaptchaResponse<ImageCaptchaVO> captchaResponse = application.generateCaptcha(CaptchaTypeConstant.SLIDER);

        return captchaResponse;
    }
    @GetMapping("/search")
    public List<User> searchUsers(@RequestParam String name) {
        return userService.searchUsers(name);
    }
    // 更新用户
    @PutMapping("/update")
    public int updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    // 查询用户
    @GetMapping("/get/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    // 查询所有用户
    @GetMapping("/list")
    public List<User> getAllUsers() {
        List<User> list = userService.getAllUsers();
        return list;
    }

}
